package org.itboys.product.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.product.entity.ProductCategory;
import org.itboys.product.entity.ShopProductCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 设置商家的经营范围（也就是设置商家所拥有的品牌和分类）
 * @author weisky
 *
 */
@Service
public class ShopProductCategoryService extends BaseService<ShopProductCategory, Long>{
	
	private static final long serialVersionUID = 4475394198024460532L;
	
	@Resource(name = "productDS")
	private MongoDataSource ds;
	@Autowired
	private ProductCategoryService productCategoryService;

	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<ShopProductCategory> getEntityClass() {
		return ShopProductCategory.class;
	}
	
	/**
	 * 根据商家ID 清除商家已分配的模板
	 * @param sellerId
	 * @return
	 */
	public boolean deleteBySellerId(Long sellerId){
		try{
			List<ShopProductCategory> spc = super.findByField("sellerId", sellerId);
			if(spc != null){
				for(ShopProductCategory spcc : spc){
					super.delete(spcc.getId());
				}
			}
			logger.warn("ShopProductCategoryService.deleteBySellerId 根据商家ID,清除商家已分配的模板成功,sellerId="+sellerId);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("ShopProductCategoryService.deleteBySellerId error:sellerId="+sellerId, e);
		}
		return false;
	}
	
	/**
	 * 根据商家ID 获取商家已分配的模板
	 * @param shopId
	 * @return
	 */
	public List<ShopProductCategory> getBySellerId(Long sellerId){
		return super.findByField("sellerId", sellerId);
	}
	
	/**
	 * 根据商家ID 获取商家已分配的模板ID
	 * @param sellerId
	 * @return
	 */
	public List<Long> getCategoryIdBySellerId(Long sellerId){
		List<ShopProductCategory> spcList = getBySellerId(sellerId);
		if(!spcList.isEmpty()){
			List<Long> ids = Lists.newArrayListWithExpectedSize(spcList.size());
			for(ShopProductCategory spc : spcList){
				ids.add(spc.getCategoryId());
			}
			return ids;
		}
		return null;
	}
	
	/**
	 * 获取商家的父分类列表
	 * @param sellerId
	 * @return
	 */
	public List<ShopProductCategory> getRootCategoryBySellerId(Long sellerId){
		Map<String,Object> param = Maps.newHashMapWithExpectedSize(2);
		param.put("sellerId", sellerId);
		param.put("categoryParentId", 0);
		List<ShopProductCategory> ll = super.list(param);
		if(ll != null){
			ll.forEach(spc -> {
				ProductCategory pc = productCategoryService.getById(spc.getCategoryId());
				spc.setCategoryName(pc.getName());
			});
		}
		return ll;
	}
	
	/**
	 * 根据商家的ID和分类的父类Id，获取下一级分类列表
	 * @param sellerId
	 * @param parentId
	 * @return
	 */
	public List<ShopProductCategory> getCategoryBySellerIdAndParentId(Long sellerId, Long parentId){
		Map<String,Object> param = Maps.newHashMapWithExpectedSize(2);
		param.put("sellerId", sellerId);
		param.put("categoryParentId", parentId);
		List<ShopProductCategory> ll = super.list(param);
		if(ll != null){
			ll.forEach(spc -> {
				ProductCategory pc = productCategoryService.getById(spc.getCategoryId());
				spc.setCategoryName(pc.getName());
			});
		}
		return ll;
	}
}
